package com.vm.osm.business.service.impl;

import com.google.gson.JsonObject;
import com.vm.osm.business.dao.OsmCheckPlanInstMapper;
import com.vm.osm.business.dao.OsmOrderEvaluateMapper;
import com.vm.osm.business.dao.OsmOrderFailureMapper;
import com.vm.osm.business.dao.OsmOrderFlowMapper;
import com.vm.osm.business.dto.CreateNotifyReq;
import com.vm.osm.business.dto.req.evaluate.ReqEvaluate;
import com.vm.osm.business.entity.OsmCheckPlanInst;
import com.vm.osm.business.entity.OsmOrderEvaluate;
import com.vm.osm.business.entity.OsmOrderFailure;
import com.vm.osm.business.entity.OsmOrderFlow;
import com.vm.osm.business.service.IEvaluateService;
import com.vm.osm.business.util.GsonUtil;
import com.vm.osm.business.util.NotifyUtil;
import com.vm.osm.business.util.UserDataUtil;
import com.vm.osm.business.util.UuidUtil;
import com.vma.eams.spring.boot.log.LogCollector;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Created by chenzui on 2018/5/9.
 */
@Service
public class EvaluateServiceImpl implements IEvaluateService {
    @Autowired
    private OsmCheckPlanInstMapper osmCheckPlanInstMapper;

    @Autowired
    private OsmOrderFailureMapper osmOrderFailureMapper;

    @Autowired
    private OsmOrderFlowMapper osmOrderFlowMapper;

    @Autowired
    private OsmOrderEvaluateMapper osmOrderEvaluateMapper;

    @Autowired
    private NotifyUtil notifyUtil;

    @Override
    public void evaluate(ReqEvaluate reqEvaluate, HttpServletRequest request) {

        if(reqEvaluate.getType().equals("1")){
            OsmOrderFailure orderFailure = osmOrderFailureMapper.selectByPrimaryKey(reqEvaluate.getOrder_id());
            orderFailure.setStatus("15");
            osmOrderFailureMapper.updateByPrimaryKeySelective(orderFailure);

            OsmOrderFailure out = osmOrderFailureMapper.selectByOutId(reqEvaluate.getOrder_id());

            if(out != null){
                out.setStatus("15");
                osmOrderFailureMapper.updateByPrimaryKeySelective(out);
                //记录操作流程节点
                OsmOrderFlow osmOrderFlow2 = new OsmOrderFlow();
                osmOrderFlow2.setSimpleDesc(reqEvaluate.getStar_content());
                osmOrderFlow2.setOperateUserId(UserDataUtil.getStaffId(request));
                osmOrderFlow2.setId(UuidUtil.getRandomUuid());
                osmOrderFlow2.setStatus(new Byte("1"));
                osmOrderFlow2.setCreateTime(Integer.valueOf(String.valueOf(System.currentTimeMillis()/1000)));
                osmOrderFlow2.setUpdateTime(Integer.valueOf(String.valueOf(System.currentTimeMillis()/1000)));
                osmOrderFlow2.setOperateUserName(UserDataUtil.getStaffName(request));
                osmOrderFlow2.setOrderId(out.getId());
                osmOrderFlow2.setOperateUserIcon(UserDataUtil.getStaffHeadImg(request));
                osmOrderFlowMapper.insertSelective(osmOrderFlow2);

                OsmOrderEvaluate evaluate2 = new OsmOrderEvaluate();
                evaluate2.setUpdateTime(new Date());
                evaluate2.setId(UuidUtil.getRandomUuid());
                evaluate2.setCreateTime(new Date());
                evaluate2.setExtInfo(reqEvaluate.getExt_info());
                evaluate2.setLabelCode(reqEvaluate.getLabel_code());
                evaluate2.setStarContent(reqEvaluate.getStar_content());
                evaluate2.setStarCode(reqEvaluate.getStar_code());
                evaluate2.setOrderId(out.getId());
                osmOrderEvaluateMapper.insertSelective(evaluate2);


                String content = "您处理的工单【"+out.getOrderTitle() +"】已评价";
                JsonObject jsonObject = new JsonObject();
                jsonObject.addProperty("order_type","1");
                jsonObject.addProperty("type","1");
                jsonObject.addProperty("order_id",out.getId());
                String extra = GsonUtil.toJson(jsonObject);
                String title = "工单被评价";
                String sender = UserDataUtil.getStaffId(request);
                String target = out.getAcceptStaffId();
                CreateNotifyReq createNotifyReq = new CreateNotifyReq(content,extra,title,sender,target,out.getEnterpriseId());

                notifyUtil.send(createNotifyReq);
            }else {
                String content = "您处理的工单【"+orderFailure.getOrderTitle() +"】已评价";
                JsonObject jsonObject = new JsonObject();
                jsonObject.addProperty("order_type","1");
                jsonObject.addProperty("type","1");
                jsonObject.addProperty("order_id",orderFailure.getId());
                String extra = GsonUtil.toJson(jsonObject);
                String title = "工单被评价";
                String sender = UserDataUtil.getStaffId(request);
                String target = orderFailure.getAcceptStaffId();
                CreateNotifyReq createNotifyReq = new CreateNotifyReq(content,extra,title,sender,target,orderFailure.getEnterpriseId());

                notifyUtil.send(createNotifyReq);
            }




        }else if(reqEvaluate.getType().equals("2")){
            OsmCheckPlanInst order = osmCheckPlanInstMapper.selectByPrimaryKey(reqEvaluate.getOrder_id());
            order.setStatus("15");
            osmCheckPlanInstMapper.updateByPrimaryKeySelective(order);

        }

        //记录操作流程节点
        OsmOrderFlow osmOrderFlow = new OsmOrderFlow();
        osmOrderFlow.setSimpleDesc(reqEvaluate.getStar_content());
        osmOrderFlow.setOperateUserId(UserDataUtil.getStaffId(request));
        osmOrderFlow.setId(UuidUtil.getRandomUuid());
        osmOrderFlow.setStatus(new Byte("1"));
        osmOrderFlow.setCreateTime(Integer.valueOf(String.valueOf(System.currentTimeMillis()/1000)));
        osmOrderFlow.setUpdateTime(Integer.valueOf(String.valueOf(System.currentTimeMillis()/1000)));
        osmOrderFlow.setOperateUserName(UserDataUtil.getStaffName(request));
        osmOrderFlow.setOrderId(reqEvaluate.getOrder_id());
        osmOrderFlow.setOperateUserIcon(UserDataUtil.getStaffHeadImg(request));
        osmOrderFlowMapper.insertSelective(osmOrderFlow);





        OsmOrderEvaluate evaluate = new OsmOrderEvaluate();
        evaluate.setUpdateTime(new Date());
        evaluate.setId(UuidUtil.getRandomUuid());
        evaluate.setCreateTime(new Date());
        evaluate.setExtInfo(reqEvaluate.getExt_info());
        evaluate.setLabelCode(reqEvaluate.getLabel_code());
        evaluate.setStarContent(reqEvaluate.getStar_content());
        evaluate.setStarCode(reqEvaluate.getStar_code());
        evaluate.setOrderId(reqEvaluate.getOrder_id());
        osmOrderEvaluateMapper.insertSelective(evaluate);




    }
}
